package com.tianshi.order.controller;

import com.tianshi.entity.OrderItems;
import com.tianshi.order.service.OrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderItem")
@Slf4j
public class OrderItemConttoller {
    @Autowired
    private OrderItemService orderItemService;


    /**
     * 查看订单项-所有的
     */
    @GetMapping("/all")
    public ResponseEntity<List<OrderItems>> getAllOrderItems() {
        log.info("查看订单项-所有的");
        List<OrderItems> orderItems = orderItemService.getAllOrderItems();
        return ResponseEntity.ok(orderItems);
    }


    /**
     * 删除订单项 -根据订单ID
     */
    @DeleteMapping("/{itemId}")
    public ResponseEntity<String> deleteOrderItem(@PathVariable("itemId") Long orderId) {
        log.info("删除订单项 -根据订单ID：{}", orderId);
        int i = orderItemService.deleteOrderItem(orderId);
        if (i > 0) {
            return ResponseEntity.ok("success");
        }
        return ResponseEntity.badRequest().body("fail");
    }

    /**
     * 根据orderId查看订单项
     */
    @GetMapping("/check/{orderId}")
    public ResponseEntity<List<OrderItems>> checkOrderItem(@PathVariable("orderId") Long orderId) {
        log.info("根据orderId查看订单项：{}", orderId);
        List<OrderItems> orderItemsList = orderItemService.checkOrderItemByOrderId(orderId);
        log.info("订单项：{}", orderItemsList);
        if (orderItemsList != null) { //订单项存在
            return ResponseEntity.ok(orderItemsList);
        }
        return ResponseEntity.badRequest().body(null);
    }


}
